/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ModelJava;

import Model.HibernateUtil;
import Model.Snowboard;
import Model.Zbozi;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author papa
 */
public class SnowboardManager implements Serializable{

    public SnowboardManager() {
    }

    protected static Session session = HibernateUtil.getSessionFactory().openSession();

    /**
     * vraci vsechny snowboardy z databaze
     * @return vsechny snowboardy
     */
    public List getAllSnowboards(){
        session.beginTransaction();
        Query q = session.createQuery("from Snowboard");
        List a =  q.list();
        session.getTransaction().commit();
        return a;
    }

    public void insertData(Snowboard s) {
        Transaction tr = null;
        try {
            tr = session.beginTransaction();
            //tady budu ukladat
            session.save(s.getZbozi());
            session.saveOrUpdate(s);
            session.getTransaction().commit();
        } catch (HibernateException he) {
            System.out.println("HQL Chyba ve vlozData");
            he.printStackTrace();
            if (tr != null) tr.rollback();
            throw he;
        }finally{
//            session.close();
        }
    }

    public Zbozi getObjectById(String id) {
        int iid = Integer.valueOf(id);
        Transaction tr = null;
        try {
            tr = session.beginTransaction();
            //tady budu ukladat
            Snowboard s = (Snowboard)session.get(Snowboard.class, iid);
            session.getTransaction().commit();
            return s;
        } catch (HibernateException he) {
            System.out.println("HQL Chyba ve vlozData");
            he.printStackTrace();
            if (tr != null) tr.rollback();
            throw he;
        }finally{
//            session.close();
        }
    }
}
